package com.heima.basic.单多线程谁快;

import java.util.Scanner;

/**
 * @Author: Ethan Yankang
 * @Program: 面试必会代码
 * @Date: 2024-09-14 22:45
 **/
public class ConcurrencyTest {
    public static void main(String[] args) throws InterruptedException {
        while (true){
        Scanner input = new Scanner(System.in);
        long count = input.nextLong();
        concurrency(count);  //单线程执行
        serial(count);       //多线程执行
    }}
    private static void concurrency(long count) throws InterruptedException {
        long start = System.currentTimeMillis();

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int a = 0;
                for (long i = 0; i < count; i++) {
                    a += 5;
                }
            }
        }); //匿名内部类的方式实现(可以用lamabd)
        thread.start();
        int b = 0;
        for (long i = 0; i < count; i++) {
            b--;
        }
        long time = System.currentTimeMillis() - start;
        thread.join();
        System.out.println("concurrency :" + time+"ms,b="+b);
    }


    private static void serial(long count) {
        long start = System.currentTimeMillis();
        int a = 0;
        for (long i = 0; i < count; i++) {
            a += 5;
        }

        int b = 0;
        for (long i = 0; i < count; i++) {
            b--;
        }
        long time = System.currentTimeMillis() - start;
        System.out.println("serial   \t:" + time+"ms,b="+b+",a="+a);
    }
}
//运行结果

//1000
//concurrency :1ms,b=-1000
//serial      :0ms,b=-1000,a=5000
//10000
//concurrency :0ms,b=-10000
//serial      :1ms,b=-10000,a=50000
//100000
//concurrency :1ms,b=-100000
//serial      :2ms,b=-100000,a=500000
//1000000
//concurrency :2ms,b=-1000000
//serial   	  :5ms,b=-1000000,a=5000000